package com.ekuaitu.kuaitu.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.adapter.CertifyPagerAdapter;
import com.ekuaitu.kuaitu.base.BaseActivity;
import com.ekuaitu.kuaitu.fragment.DepositCarFragment;
import com.ekuaitu.kuaitu.fragment.IdcardFragment;
import com.ekuaitu.kuaitu.fragment.LisenceFragment;
import com.ekuaitu.kuaitu.utils.SharedPreferencesKey;
import com.ekuaitu.kuaitu.widget.NoScrollViewPager;
import com.ekuaitu.kuaitu.utils.SharedPreferencesUtils;
import com.ekuaitu.kuaitu.utils.StatusBarUtil;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CarCertificateActivity extends BaseActivity {

    @BindView(R.id.return_certificate)
    ImageView returnCertificate;
    @BindView(R.id.tv_certificate_title)
    TextView tvCertificateTitle;
    @BindView(R.id.toolbar_certificate)
    Toolbar toolbarCertificate;
    @BindView(R.id.image_dot1)
    ImageView imageDot1;
    @BindView(R.id.image_dot2)
    ImageView imageDot2;
    @BindView(R.id.image_dot3)
    ImageView imageDot3;
    @BindView(R.id.image_dot4)
    ImageView imageDot4;
    @BindView(R.id.certificate_ll_jbzj)
    public  AutoLinearLayout certificateLlJbzj;
    @BindView(R.id.image_dot11)
    ImageView imageDot11;
    @BindView(R.id.image_dot22)
    ImageView imageDot22;
    @BindView(R.id.image_dot33)
    ImageView imageDot33;
    @BindView(R.id.image_dot44)
    ImageView imageDot44;
    @BindView(R.id.certificate_ll_smrz)
    public AutoLinearLayout certificateLlSmrz;
    @BindView(R.id.image_dot111)
    ImageView imageDot111;
    @BindView(R.id.image_dot222)
    ImageView imageDot222;
    @BindView(R.id.image_dot333)
    ImageView imageDot333;
    @BindView(R.id.image_dot444)
    ImageView imageDot444;
    @BindView(R.id.certificate_ll_zzhy)
    public AutoLinearLayout certificateLlZzhy;
    @BindView(R.id.image_dot1111)
    ImageView imageDot1111;
    @BindView(R.id.image_dot2222)
    ImageView imageDot2222;
    @BindView(R.id.image_dot3333)
    ImageView imageDot3333;
    @BindView(R.id.image_dot4444)
    ImageView imageDot4444;
    @BindView(R.id.certificate_ll_jbzj10)
    public AutoLinearLayout certificateLlJbzj10;
    @BindView(R.id.image_dot11111)
    ImageView imageDot11111;
    @BindView(R.id.image_dot22222)
    ImageView imageDot22222;
    @BindView(R.id.image_dot33333)
    ImageView imageDot33333;
    @BindView(R.id.image_dot44444)
    ImageView imageDot44444;
    @BindView(R.id.certificate_ll_jbzj11)
    public AutoLinearLayout certificateLlJbzj11;
    @BindView(R.id.fragment_vp)
    NoScrollViewPager fragmentVp;
    private List<Fragment> totalList;
    private int curIndex;
    private Context mContext = this;
    public NoScrollViewPager mVp;
    private CertifyPagerAdapter adapter;
    public TextView tvTitle;
    private CarCertificateActivity activity = this;
    private String identityStatus;
    private String licenseStatus;
    private String depositStatus;

    @Override
    public int getLayoutId() {
        return R.layout.activity_certificate;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimaryWhite), 0,true);
    }

    @Override
    public void initData() {
        // 碎片经理和事务
        // 将碎片添加进集合中

        mVp = (NoScrollViewPager) findViewById(R.id.fragment_vp);
        tvTitle = (TextView) findViewById(R.id.tv_certificate_title);
        mVp.setNoScroll(true);
        totalList = new ArrayList<>();
        totalList.add(new DepositCarFragment());
        totalList.add(new IdcardFragment());
        totalList.add(new LisenceFragment());
        adapter = new CertifyPagerAdapter(getSupportFragmentManager(), totalList);
        mVp.setAdapter(adapter);
        identityStatus = SharedPreferencesUtils.getInstance(mContext).getStringValue(SharedPreferencesKey.IDENTITY_STATUS);
        licenseStatus = SharedPreferencesUtils.getInstance(mContext).getStringValue(SharedPreferencesKey.LICENSE_STATUS);

        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                Log.i("TAGn", position + "");
                if (position == 1) {
                    certificateLlJbzj.setVisibility(View.GONE);
                    certificateLlSmrz.setVisibility(View.VISIBLE);
                    certificateLlZzhy.setVisibility(View.GONE);
                    certificateLlJbzj10.setVisibility(View.GONE);
                    certificateLlJbzj11.setVisibility(View.GONE);
                    tvTitle.setText("实名认证");
                } else if (position == 2) {
                    certificateLlJbzj.setVisibility(View.GONE);
                    certificateLlSmrz.setVisibility(View.GONE);
                    certificateLlZzhy.setVisibility(View.VISIBLE);
                    certificateLlJbzj10.setVisibility(View.GONE);
                    certificateLlJbzj11.setVisibility(View.GONE);
                    tvTitle.setText("证照核验");
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });


        Intent intent = getIntent();
        int flag = intent.getIntExtra("SubmitFlag", -1);

        switch (flag) {
            //实名认证
            case 1:
                mVp.setCurrentItem(1);
                break;
            //证照核验
            case 2:
                mVp.setCurrentItem(2);
                break;
            //保证金
            case 3:
                mVp.setCurrentItem(0);
                deposit();
                break;
        }

    }

    //viewpager的监听器打开位置为0的 mVp.setCurrentItem(0)不会触发监听器。
    private void deposit() {
        tvTitle.setText("缴保证金");
        switch (identityStatus) {
            //身份证审核中
            //身份证审核成功
            case "11":
            case "13":
                switch (licenseStatus) {
                    //驾驶证未审核
                    //驾驶证失败
                    case "10":
                    case "12":
                        certificateLlJbzj.setVisibility(View.GONE);
                        certificateLlSmrz.setVisibility(View.GONE);
                        certificateLlZzhy.setVisibility(View.GONE);
                        certificateLlJbzj10.setVisibility(View.VISIBLE);
                        certificateLlJbzj11.setVisibility(View.GONE);
                        break;
                    //驾驶证审核中
                    //驾驶证审核成功
                    case "11":
                    case "13":
                        certificateLlJbzj.setVisibility(View.GONE);
                        certificateLlSmrz.setVisibility(View.GONE);
                        certificateLlZzhy.setVisibility(View.GONE);
                        certificateLlJbzj10.setVisibility(View.GONE);
                        certificateLlJbzj11.setVisibility(View.VISIBLE);
                        break;
                }
                break;
            //身份证未审核
            //身份证失败
            case "10":
            case "12":
                switch (licenseStatus) {
                    //驾驶证未审核
                    //驾驶证失败
                    //驾驶证审核中
                    //驾驶证审核成功
                    case "11":
                    case "13":
                    case "10":
                    case "12":
                        certificateLlJbzj.setVisibility(View.VISIBLE);
                        certificateLlSmrz.setVisibility(View.GONE);
                        certificateLlZzhy.setVisibility(View.GONE);
                        certificateLlJbzj10.setVisibility(View.GONE);
                        certificateLlJbzj11.setVisibility(View.GONE);
                        break;
                }
                break;
        }
    }

    @OnClick(R.id.return_certificate)
    public void onClick() {
        finish();
    }

}
